package com.demo.business;

import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.demo.R;
import com.keguoyu.easymvp.annotation.Receiver;
import com.keguoyu.easymvp.business.Business;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.BehaviorSubject;

public class LoginButtonBusiness extends Business {

    private Button loginButton;

    @Receiver("name")
    BehaviorSubject<String> nameSubject;

    @Receiver("password")
    BehaviorSubject<String> passwordSubject;

    @Override
    protected void doFindViewById(@NonNull View view) {
        super.doFindViewById(view);
        loginButton = (Button) bindWidget(view, R.id.login_button);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        addToAutoDisposable(nameSubject.subscribe(s -> {
            if (s.equals("keguoyu")) {
                loginButton.setText("登录中。。。");
                addToAutoDisposable(Observable
                        .empty()
                        .delay(2, TimeUnit.SECONDS)
                        .doOnComplete(new Action() {
                            @Override
                            public void run() throws Exception {
                                Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .subscribe());
            }
        }));
    }
}
